import javax.swing.*;
import javax.swing.tree.*;
import java.awt.GridLayout;

/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * <code>JPanel</code> that contains a tree structure for a folder
 * </P>
 * @version 0.1
 */
public class FavoritesTreePanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1362947731605755132L;
	
	@SuppressWarnings("unused")
	private OrganizerModel model;
	
	DefaultMutableTreeNode root = new DefaultMutableTreeNode("favorites") ;
	DefaultMutableTreeNode folder1, folder2;
	
	private DefaultTreeModel favoritesTreeModel;
	
	private JTree favoritesTree;
	
	
	public FavoritesTreePanel() {
		this.setLayout( new GridLayout(1,1) );
		
		favoritesTreeModel = new DefaultTreeModel(root);
		
		favoritesTree = new JTree( favoritesTreeModel );
		
		this.add( new JScrollPane(favoritesTree) );
		
		updateTree();
	}
	
	public void updateTree() {
		
		((DefaultTreeModel)favoritesTree.getModel()).reload();
	}
	
	public void addBook(EBook book) {
		DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)
			favoritesTree.getLastSelectedPathComponent();
		
		if (parentNode == null) {
			System.out.println("addNode: no Parent Selected");
			return;
		}
		
		try {
			parentNode.add( new DefaultMutableTreeNode(book, false));
			
		}
		catch(IllegalStateException ex) {
			System.out.println(ex);
		}
		finally {
			favoritesTreeModel.reload();
		}
		
	}
	
	public void addFolder(Folder folder) {
		DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)
		favoritesTree.getLastSelectedPathComponent();
	
		if (parentNode == null) {
			System.out.println("addNode: no Parent Selected");
			return;
		}
		
		try {
			
			parentNode.setAllowsChildren( true );
			parentNode.add( new DefaultMutableTreeNode(folder, true));

			System.out.println("Not a Folder");
		}
		catch(IllegalStateException ex) {
			System.out.println("Must Select a Folder");
		}
		finally {
			favoritesTreeModel.reload();
		}
	}
	
	public void setModel(OrganizerModel model) {
		this.model = model;
	}
	
	
}
